<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="申报类型" prop="typeId">
                    <el-select
                    v-model="queryParams.typeId"
                    filterable
                    remote
                    clearable
                    reserve-keyword
                    placeholder="请选择申报类型"
                    style="width: 383px"
                    :remote-method="remoteLocationMethod"
                    :loading="loading">
                    <el-option
                        v-for="item in declarationTypeData"
                        :key="item.id"
                        :label="item.typeName"
                        :value="item.id">
                    </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="申报条件" prop="acceptancerequirments">
                    <el-input class="w-[280px]" v-model="queryParams.acceptancerequirments" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['declaration:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists" stripe>
                <el-table-column label="编号" prop="id" min-width="100" />
                <el-table-column label="申报类型" prop="typeName" min-width="100" />
                <el-table-column label="符号" prop="symbol" min-width="100" />
                <el-table-column label="申报条件" prop="acceptancerequirments" min-width="100" />
                <el-table-column label="修改人" prop="modifier" min-width="100" />
                <el-table-column label="创建人" prop="creator" min-width="100" />
                <el-table-column label="创建时间" prop="createTime" min-width="100" />
                <el-table-column label="更新时间" prop="updateTime" min-width="100" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['declaration:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['declaration:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="declaration">
import { declarationDelete, declarationLists } from '@/api/declaration'
import { declarationTypeLists } from '@/api/declarationType'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    modifier: '',
    creator: '',
    code: '',
    acceptancerequirments: '',
    typeId:''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: declarationLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await declarationDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

const declarationTypeData = ref([]);
//获取申报类型
const paramsType = reactive({
        pageNo: 1,
        pageSize: 60,
        typeName:null
        })
const getDeclarationType = async () =>{

    try {
        await declarationTypeLists(paramsType).then((res)=>{
            declarationTypeData.value=res.lists
            return  declarationTypeData.value
        })
      } catch (error) {
        console.error('获取数据失败:', error);
      }
}
//搜索申报类型
const remoteLocationMethod=async (query)=>{
      paramsType.typeName=query
      declarationTypeLists(paramsType).then((res)=>{
        declarationTypeData.value=res.lists
        return  declarationTypeData.value
      })
    }

getLists()
getDeclarationType()
</script>
